from docx import Document
from docx.shared import Pt, RGBColor, Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH

# Predefined styles
predefined_styles = {
    'Normal': {
        'font': {
            'name': 'Arial',
            'size': Pt(12),
            'color': RGBColor(0, 0, 0),
            'underline': False,
            'alignment': 'left'
        },
        'first_line_indent': Inches(0.5),
        'line_spacing': Pt(15),
        'background_color': 'FFFFFF'
    },
    'Heading1': {
        'font': {
            'name': 'Arial',
            'size': Pt(24),
            'color': RGBColor(0, 0, 0),
            'underline': False,
            'alignment': 'center'
        },
        'background_color': 'FFFFFF'
    },
    'Heading2': {
        'font': {
            'name': 'Arial',
            'size': Pt(18),
            'color': RGBColor(0, 128, 0),
            'underline': True,
            'alignment': 'left'
        },
        'background_color': 'FFFFFF'
    },
    'CustomStyle1': {
        'font': {
            'name': 'Times New Roman',
            'size': Pt(14),
            'color': RGBColor(255, 0, 0),
            'underline': True,
            'alignment': 'center'
        }
    },
    'Quote': {
        'font': {
            'name': 'Georgia',
            'size': Pt(14),
            'color': RGBColor(128, 128, 128),
            'underline': False,
            'alignment': 'center'
        },
        'first_line_indent': Inches(0.5),
        'line_spacing': Pt(20)
    },
    'Code': {
        'font': {
            'name': 'Courier New',
            'size': Pt(10),
            'color': RGBColor(0, 0, 0),
            'underline': False,
            'alignment': 'left'
        },
        'first_line_indent': Inches(0),
        'line_spacing': Pt(15)
    },
    'Title': {
        'font': {
            'name': 'Arial Black',
            'size': Pt(36),
            'color': RGBColor(0, 0, 0),
            'underline': True,
            'alignment': 'center'
        },
        'background_color': 'FFFFFF'
    },
    'Subtitle': {
        'font': {
            'name': 'Arial',
            'size': Pt(24),
            'color': RGBColor(0, 0, 128),
            'underline': False,
            'alignment': 'center'
        },
        'background_color': 'FFFFFF'
    },
    'Emphasis': {
        'font': {
            'name': 'Verdana',
            'size': Pt(12),
            'color': RGBColor(0, 0, 0),
            'underline': False,
            'alignment': 'left'
        },
        'first_line_indent': Inches(0.5),
        'line_spacing': Pt(15),
        'background_color': 'FFFFE0'
    },
    'Highlight': {
        'font': {
            'name': 'Verdana',
            'size': Pt(12),
            'color': RGBColor(0, 0, 0),
            'underline': False,
            'alignment': 'left'
        },
        'first_line_indent': Inches(0.5),
        'line_spacing': Pt(15),
        'background_color': 'FFDDC1'
    },
    'Note': {
        'font': {
            'name': 'Calibri',
            'size': Pt(11),
            'color': RGBColor(0, 0, 0),
            'underline': False,
            'alignment': 'left'
        },
        'first_line_indent': Inches(0.25),
        'line_spacing': Pt(13),
        'background_color': 'E7F4E4'
    }
}

def apply_style(paragraph, style_name):
    """Apply the predefined style to the given paragraph."""
    style = predefined_styles.get(style_name, predefined_styles['Normal'])
    run = paragraph.add_run()
    font = run.font
    font.name = style['font']['name']
    font.size = style['font']['size']
    font.color.rgb = style['font']['color']
    font.underline = style['font']['underline']
    if 'first_line_indent' in style:
        paragraph.paragraph_format.first_line_indent = style['first_line_indent']
    if 'line_spacing' in style:
        paragraph.paragraph_format.line_spacing = style['line_spacing']
    # Set alignment
    if style['font']['alignment'] == 'left':
        paragraph.alignment = WD_ALIGN_PARAGRAPH.LEFT
    elif style['font']['alignment'] == 'center':
        paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
    elif style['font']['alignment'] == 'right':
        paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT

# Create a sample document
doc = Document()
doc.add_heading('Document Title', level=1)

# Add a paragraph with Normal style
p = doc.add_paragraph()
run = p.add_run('This is a normal paragraph.')
apply_style(p, 'Normal')

# Add a paragraph with Heading1 style
p = doc.add_paragraph()
run = p.add_run('This is a heading 1 paragraph.')
apply_style(p, 'Heading1')

# Add a paragraph with CustomStyle1 style
p = doc.add_paragraph()
run = p.add_run('This is a custom style 1 paragraph.')
apply_style(p, 'CustomStyle1')

# Add a paragraph with Quote style
p = doc.add_paragraph()
run = p.add_run('This is a quote paragraph.')
apply_style(p, 'Quote')

# Add a paragraph with Code style
p = doc.add_paragraph()
run = p.add_run('This is a code paragraph.')
apply_style(p, 'Code')

# Save the document
doc.save('styled_document.docx')